import os

def create_template_files():
    # 确保目录存在
    os.makedirs('app/templates/main', exist_ok=True)
    
    # 创建 base.html
    base_html = '''<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block title %}{% endblock %} - 新疆优质拉面籽粒系统</title>
    
    <!-- CSS -->
    <link rel="stylesheet" href="{{ url_for('static', filename='css/bootstrap.min.css') }}">
    <link rel="stylesheet" href="{{ url_for('static', filename='css/dataTables.bootstrap5.min.css') }}">
    <link rel="stylesheet" href="{{ url_for('static', filename='css/custom.css') }}">
    
    <!-- JavaScript -->
    <script src="{{ url_for('static', filename='js/jquery.min.js') }}"></script>
    <script src="{{ url_for('static', filename='js/bootstrap.bundle.min.js') }}"></script>
    <script src="{{ url_for('static', filename='js/jquery.dataTables.min.js') }}"></script>
    <script src="{{ url_for('static', filename='js/dataTables.bootstrap5.min.js') }}"></script>
    <script src="{{ url_for('static', filename='js/chart.min.js') }}"></script>
    <script src="{{ url_for('static', filename='js/asset-paths.js') }}"></script>
</head>
<body>
    {% block content %}{% endblock %}
</body>
</html>'''

    # 创建 index.html
    index_html = '''{% extends "base.html" %}

{% block title %}首页{% endblock %}

{% block content %}
<!-- 导航栏 -->
<nav class="navbar navbar-expand-lg navbar-light bg-light border-bottom shadow-sm">
    <div class="container">
        <a class="navbar-brand" href="{{ url_for('main.index') }}">
            <span class="text-primary">新疆优质拉面</span>
        </a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNav">
            <ul class="navbar-nav me-auto">
                <li class="nav-item">
                    <a class="nav-link" href="{{ url_for('data.index') }}">数据采集</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="{{ url_for('analysis.index') }}">分子标记分析</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="{{ url_for('reports.index') }}">报告生成</a>
                </li>
            </ul>
            <div class="d-flex">
                {% if current_user.is_authenticated %}
                <div class="dropdown">
                    <button class="btn btn-outline-primary dropdown-toggle" type="button" data-bs-toggle="dropdown">
                        {{ current_user.username }}
                    </button>
                    <ul class="dropdown-menu dropdown-menu-end">
                        <li><a class="dropdown-item" href="#">个人信息</a></li>
                        <li><hr class="dropdown-divider"></li>
                        <li><a class="dropdown-item" href="{{ url_for('auth.logout') }}">退出登录</a></li>
                    </ul>
                </div>
                {% else %}
                <a href="{{ url_for('auth.login') }}" class="btn btn-outline-primary me-2">登录</a>
                {% endif %}
                <button class="btn btn-outline-secondary ms-2" id="darkModeToggle">
                    <i class="fas fa-moon"></i>
                </button>
            </div>
        </div>
    </div>
</nav>

<!-- 主要内容 -->
<main class="container mt-4">
    <!-- 欢迎区域 -->
    <div class="p-5 mb-4 bg-light rounded-3">
        <div class="container-fluid py-5">
            <h1 class="display-5 fw-bold">新疆优质拉面籽粒系统</h1>
            <p class="col-md-8 fs-4">
                专业的籽粒硬度及Pins基因分子标记改良技术体系平台，助力新疆优质拉面研究与改良。
            </p>
            {% if not current_user.is_authenticated %}
            <a href="{{ url_for('auth.login') }}" class="btn btn-primary btn-lg">开始使用</a>
            {% endif %}
        </div>
    </div>

    <!-- 功能卡片 -->
    <div class="row row-cols-1 row-cols-md-3 g-4 mb-4">
        <div class="col">
            <div class="card h-100">
                <div class="card-body">
                    <h5 class="card-title">数据采集</h5>
                    <p class="card-text">支持实验数据的录入与管理，包括籽粒硬度测定数据、Pins基因序列数据等。</p>
                    <a href="{{ url_for('data.index') }}" class="btn btn-outline-primary">进入</a>
                </div>
            </div>
        </div>
        <div class="col">
            <div class="card h-100">
                <div class="card-body">
                    <h5 class="card-title">分子标记分析</h5>
                    <p class="card-text">基于分子标记技术，进行Pins基因检测和籽粒硬度分析。</p>
                    <a href="{{ url_for('analysis.index') }}" class="btn btn-outline-primary">进入</a>
                </div>
            </div>
        </div>
        <div class="col">
            <div class="card h-100">
                <div class="card-body">
                    <h5 class="card-title">报告生成</h5>
                    <p class="card-text">生成实验数据分析报告和改良技术推荐报告。</p>
                    <a href="{{ url_for('reports.index') }}" class="btn btn-outline-primary">进入</a>
                </div>
            </div>
        </div>
    </div>
</main>

<!-- 页脚 -->
<footer class="footer mt-auto py-3 bg-light">
    <div class="container text-center">
        <span class="text-muted">© 2024 新疆优质拉面籽粒系统. All rights reserved.</span>
    </div>
</footer>

<!-- 暗色模式切换脚本 -->
<script>
document.getElementById('darkModeToggle').addEventListener('click', function() {
    document.body.classList.toggle('dark-mode');
    const isDarkMode = document.body.classList.contains('dark-mode');
    localStorage.setItem('darkMode', isDarkMode);
});

// 检查本地存储中的暗色模式设置
if (localStorage.getItem('darkMode') === 'true') {
    document.body.classList.add('dark-mode');
}
</script>
{% endblock %}'''

    # 写入文件
    with open('app/templates/base.html', 'w', encoding='utf-8') as f:
        f.write(base_html)
    
    with open('app/templates/main/index.html', 'w', encoding='utf-8') as f:
        f.write(index_html)

if __name__ == '__main__':
    create_template_files() 